<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('店铺订单列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="case-form">
                <div class="select-list">
                    <ul>
                        <li>
                            <p style="width: 110px">订单号： </p>
                            <input type="text" name="orderSn"/>
                        </li>
                        <li>
                            <p style="width: 110px;">会员编号：</p>
                            <input type="text" name="memberSn"/>
                        </li>
                        <li>
                            <p style="width: 110px">订单状态： </p>
                            <select name="orderStatus" th:with="type=${@dict.getType('order_status')}">
                                <option value="-1">全部</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <p style="width: 110px">用户付款状态： </p>
                            <select name="payStatus" th:with="type=${@dict.getType('pay_status')}">
                                <option value="-1">全部</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <p style="width: 110px">店铺付款状态： </p>
                            <select name="shopPayStatus" th:with="type=${@dict.getType('pay_status')}">
                                <option value="-1">全部</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li class="select-time">
                            <p style="width: 110px">下单时间： </p>
                            <input type="text" class="time-input" placeholder="开始时间"
                                   name="params[beginTime]"/>
                            <span>-</span>
                            <input type="text" class="time-input" placeholder="结束时间"
                                   name="params[endTime]"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success single disabled " onclick="changeShopRemark()"
               shiro:hasPermission="back:shopOrder:shopRemark">
                <i class="fa fa-edit"></i> 编辑商家备注
            </a>
            <a class="btn btn-warning" onclick="pay()" shiro:hasPermission="back:shopOrder:pay">
                <i class="fa fa-money"></i> 批量付款
            </a>
            <a class="btn btn-warning multiple disabled" onclick="exportSelect()"
               shiro:hasPermission="back:shopOrder:export">
                <i class="fa fa-download"></i> 选择导出
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    const prefix = ctx + "back/shopOrder";
    const payFlag = [[${@permission.hasPermi('back:shopOrder:pay')}]];
    const editFlag = [[${@permission.hasPermi('back:shopOrder:edit')}]];
    const closeFlag = [[${@permission.hasPermi('back:shopOrder:cancel')}]];
    const removeFlag = [[${@permission.hasPermi('back:shopOrder:remove')}]];
    const orderStatusDict = [[${@dict.getType('order_status')}]];
    const stockStatusDict = [[${@dict.getType('stock_status')}]];
    const payStatusDict = [[${@dict.getType('pay_status')}]];

    $(function () {
        var options = {
            height:600,
            url: prefix + "/searchList",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            detailUrl: "/back/order/detail/{id}",
            exportUrl: prefix + "/export",
            modalName: "店铺订单",
            sortName: "createTime",
            sortOrder: "desc",
            showColumns: true,
            paginationShowPageGo: true,
            detailView: true,
            clickToSelect: true,
            onExpandRow: function (index, row, $detail) {
                initChildTable(index, row, $detail);
            },
            columns: [
                {
                    checkbox: true
                },
                {
                    field: 'orderSn',
                    title: '订单号',
                    formatter: function (value, row, index) {
                        return '<a style="text-decoration:underline" href="javascript:void(0)" onclick="$.operate.detail(\'' + value + '\',1000)">' + value + '</a>';
                    }
                },
                {
                    field: 'memberSn',
                    title: '用户',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<span style="font-weight: bold">' + row.memberSn + '</span><br/>');
                        let imgHtml = $.common.sprintf("<img class='img-circle img-xs' data-height='%s' data-width='%s' data-target='%s'  style='border-radius: 0;' src='%s'/>", 500, 'auto', 'self', row.avatar);
                        actions.push(imgHtml);
                        actions.push('<br/><span style="font-weight: bold">' + row.nickname + '</span>');
                        return actions.join('');
                    }
                },
                {
                    field: 'orderStatus',
                    title: '订单状态',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(orderStatusDict, value);
                    }
                },
                {
                    field: 'buyCost',
                    title: '金额',
                    width: '18%',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<label>进货总价：</label><span style="font-weight: bold">' + value + '</span>');
                        actions.push('<br/><label>商品总价：</label><span style="font-weight: bold">' + row.productCost + '</span>');
                        actions.push('<br/><label>物流费：</label><span style="font-weight: bold">' + row.deliveryCost + '</span>');
                        actions.push('<br/><label>实付金额：</label><span style="font-weight: bold">' + row.payCost + '</span>');
                        return actions.join('');
                    }
                },
                {
                    field: 'detailAddress',
                    title: '收货信息',
                    width: '18%',
                    visible: false,
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<label>收货人：</label><span style="font-weight: bold">' + row.receiver + '</span>');
                        actions.push('<br/><label>收货电话：</label><span style="font-weight: bold">' + row.receiverPhone + '</span>');
                        actions.push('<br/><label>详细地址：</label><span style="font-weight: bold">' + row.receiveAddress + '</span>');
                        return actions.join('');
                    }
                },
                {
                    field: 'payStatus',
                    title: '用户付款信息',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<label>付款状态：</label><span style="font-weight: bold">' + $.table.selectDictLabel(payStatusDict, value) + '</span>');
                        if (parseInt(value) === 1) {
                            actions.push('<br/><label>付款时间：</label><span style="font-weight: bold">' + row.payTime + '</span>');
                        }
                        return actions.join('');
                    }
                },
                {
                    field: 'shopPayStatus',
                    title: '店铺付款信息',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<label>付款状态：</label><span style="font-weight: bold">' + $.table.selectDictLabel(payStatusDict, value) + '</span>');
                        if (parseInt(value) === 1) {
                            actions.push('<br/><label>付款时间：</label><span style="font-weight: bold">' + row.shopPayTime + '</span>');
                        }
                        return actions.join('');
                    }
                },
                {
                    field: 'isEnable',
                    title: '库存状态',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(stockStatusDict, value);
                    }
                },
                {
                    field: 'orderRemark',
                    title: '买家备注'
                },
                {
                    field: 'shopRemark',
                    title: '商家备注',
                    visible:false
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    sortable: true
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        if (1 === parseInt(row.payStatus) && 0 === parseInt(row.shopPayStatus) && 1 === parseInt(row.isEnable) && 1 === parseInt(row.orderStatus)) {
                            actions.push('<a class="btn btn-warning btn-xs ' + payFlag + '" href="#" onclick="pay(\'' + row.orderSn + '\')"><i class="fa fa-money">付款</i></a> ');
                        }
                        if (1 === parseInt(row.payStatus) && (0 === parseInt(row.shopPayStatus) || 5 === parseInt(row.shopPayStatus)) && 1 === parseInt(row.orderStatus)) {
                            actions.push('<a class="btn btn-warning btn-xs ' + closeFlag + '" href="#" onclick="cancel(\'' + row.orderSn + '\')"><i class="fa fa-close">取消订单</i></a> ');
                        }
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="$.operate.edit(\'' + row.orderSn + '\')"><i class="fa fa-edit">编辑</i></a> ');
                        // actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-trash"></i>删除</a>');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });
    initChildTable = function (index, row, $detail) {
        var cur_table = $detail.html('<table style="table-layout:fixed"></table>').find('table');
        $(cur_table).bootstrapTable({
            url: prefix + "/detailList",
            method: 'post',
            sidePagination: "server",
            contentType: "application/x-www-form-urlencoded",   // 编码类型
            queryParams: {
                orderSn: row.orderSn
            },
            columns: [
                {
                    field: 'id',
                    title: 'ID',
                    visible: false
                },
                {
                    field: 'productSn',
                    title: '编号',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<label>商品编号：</label><span style="font-weight: bold">' + value + '</span>');
                        if (parseInt(row.productSkuId) !== 0 && row.productSkuCode != null) {
                            actions.push('<br/><label>sku编号：</label><span style="font-weight: bold">' + row.productSkuCode + '</span>');
                        }
                        return actions.join('');
                    }
                },
                {
                    field: 'productName',
                    title: '商品信息',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<span>' + value + '</span>');
                        if (parseInt(row.productSkuId) !== 0 && row.productAttr != null) {
                            let skuName = "";
                            let skuJsonData = JSON.parse(row.productAttr);
                            for (let i = 0; i < skuJsonData.length; i++) {
                                skuName += skuJsonData[i].key + ":" + skuJsonData[i].value + ";"
                            }
                            actions.push('<br/><span style="font-weight: bold">' + skuName + '</span>');
                        }
                        return actions.join('');
                    }
                },
                {
                    field: 'productCover',
                    title: '封面图',
                    formatter: function (value, row, index) {
                        // var thumbUrl = value + '?imageView2/1/w/64/h/64';
                        return $.common.sprintf("<img class='img-circle img-sm' data-height='%s' data-width='%s' data-target='%s'  style='border-radius: 0;' src='%s' data-src='%s'/>", 400, 'auto', 'self', value, value);
                    }
                },
                {
                    field: 'productBuyPrice',
                    title: '店家应付' +
                        '',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<label>进货价：</label><span style="font-weight: bold">' + value + '</span>');
                        actions.push('<br/><label>数量：</label><span style="font-weight: bold">' + row.productQuantity + '</span>');
                        actions.push('<br/><label>进货总价：</label><span style="font-weight: bold">' + row.totalBuyPrice + '</span>');
                        return actions.join('');
                    }
                },
                {
                    field: 'productSkuPrice',
                    title: '店家售价',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<label>销售价：</label><span style="font-weight: bold">' + value + '</span>');
                        actions.push('<br/><label>数量：</label><span style="font-weight: bold">' + row.productQuantity + '</span>');
                        actions.push('<br/><label>销售总价：</label><span style="font-weight: bold">' + row.totalBuyPrice + '</span>');
                        return actions.join('');
                    }
                },
                {
                    field: 'remark',
                    title: '商品备注'
                }
            ]
        });
    };

    /* 修改商家备注 */
    function changeShopRemark() {
        var orderSnArr = $.table.selectColumns('orderSn');
        var url = prefix + '/shopRemark/' + orderSnArr[0];
        $.modal.open("修改商家备注", url);
    }

    function pay(orderSn) {
        var url = ctx + 'back/shopOrder/pay/';
        if (orderSn == undefined) {
            var rows = $.table.selectFirstColumns();
            if (rows.length == 0) {
                $.modal.alertWarning("请至少选择一张订单支付");
                return;
            }
            url += rows.join();
        } else {
            url += orderSn;
        }
        //$.modal.open("订单批量支付", url, 1000);
        var options = {
            title: '订单批量支付',
            width: "1000",
            url: url,
            btn:['关闭'],
            yes: function (index, layero) {
                layer.close(index);
            }
        };
        $.modal.openOptions(options);
    }

    //选择数据导出
    function exportSelect() {
        var rows = $.table.selectColumns("orderSn");
        if (rows.length === 0) {
            $.modal.alertWarning("请至少选择一条记录");
            return;
        }
        $.modal.confirm("确认要导出的" + rows.length + "条数据吗？", function () {
            var data = {};
            if (rows.length === 1) {
                data.orderSn = rows[0];
            } else {
                data.orderSnStr = rows.join()
            }
            $.modal.loading("正在导出数据，请稍后...");
            $.post(prefix + "/export", data, function (result) {
                if (result.code == web_status.SUCCESS) {
                    window.location.href = ctx + "common/download?fileName=" + encodeURI(result.msg) + "&delete=" + true;
                } else if (result.code == web_status.WARNING) {
                    $.modal.alertWarning(result.msg)
                } else {
                    $.modal.alertError(result.msg);
                }
                $.modal.closeLoading();
            });
        });
    }

    /* 订单取消 */
    function cancel(orderSn) {
        $.modal.confirm("确认取消订单" + orderSn + "吗？", function () {
            $.post(prefix + "/cancel", {orderSn: orderSn,cancelReason: "库存不足"}, function (result) {
                if (result.code == web_status.SUCCESS) {
                    $.modal.alertSuccess(result.msg)
                    $.table.refresh();
                } else if (result.code == web_status.WARNING) {
                    $.modal.alertWarning(result.msg)
                } else {
                    $.modal.alertError(result.msg);
                }
                $.modal.closeLoading();
            });
        });
    }

    //自定义导出
    function exportCustom() {
        var url = prefix + '/exportCustom';
        $.modal.open("订单自定义导出", url);
    }
</script>
</body>
</html>